package com.yonder.controller;

import com.yonder.common.MessageConstant;
import com.yonder.entity.PageResult;
import com.yonder.entity.QueryPageBean;
import com.yonder.entity.Result;
import com.yonder.pojo.CheckItem;
import com.yonder.service.CheckItemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName CheckItemsController
 * @Description TODO
 * @Author zxs
 * @Date 2021/3/19 18:01
 * Version 1.0
 */
@RestController
@RequestMapping("/checkitem")
public class CheckItemsController {

    @Autowired
    private CheckItemsService checkItemsService;
    /*添加检查项*/
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result add(@RequestBody CheckItem checkItem){
        try {
//            System.out.println("CheckItemsController==========");
            checkItemsService.add(checkItem);
            return  new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL );
    }

    /*
    *检查项分页
    *
    * */
    @RequestMapping(value = "/findPage",method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")//权限校验
    public PageResult find(@RequestBody QueryPageBean queryPageBean){
        try {
            System.out.println("findPage===");
            return  checkItemsService.findPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /*添加检查项*/
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public Result deleteById( Integer id){
        try {
            System.out.println("CheckItemsController==========");
            checkItemsService.deleteById(id);
            return  new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL );
    }


    /*
    * 修改检查项
    * */
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public Result updateItem(@RequestBody CheckItem checkItem){
        try {
//            System.out.println("CheckItemsController==========");
            checkItemsService.edit(checkItem);
            return  new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL );
    }

    /*
     *检查项分页
     *
     * */
    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    public Result findAll(){
        try {
            List<CheckItem> all = checkItemsService.findAll();
            return  new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS,all);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
    }
}
